package com.wx.resume_excel.listener;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.ArrayList;
import java.util.List;

/**
 * 文档中其他sheet页数据单元格设置的样式设置的监听类
 *
 * @Auther 田艺鹏
 * @DateTime 2020-08-01 09:14
 */
public class CustomCellWriteHandler implements CellWriteHandler {

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean isHead) {
        Workbook workbook = cell.getSheet().getWorkbook();
        Sheet sheet = cell.getSheet();
        sheet.setColumnWidth(0,20*256);
        sheet.setColumnWidth(1,20*768);
        sheet.setHorizontallyCenter(true);

        /**
         * 通用style
         */
        CellStyle cellStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 9);
        font.setFontName("宋体");
        cellStyle.setFont(font);

        cellStyle.setBorderBottom(BorderStyle.THICK); //下边框
        cellStyle.setBorderLeft(BorderStyle.THICK);//左边框
        cellStyle.setBorderTop(BorderStyle.THICK);//上边框
        cellStyle.setBorderRight(BorderStyle.THICK);//右边框
        cellStyle.setBottomBorderColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyle.setTopBorderColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.PALE_BLUE.getIndex());

        /**
         * 根据行数信息进行背景色填充
         */
        if(cell.getRowIndex() == 7 || cell.getRowIndex() == 24 || cell.getRowIndex() == 30 || cell.getRowIndex() == 39
                || cell.getRowIndex() == 50 || cell.getRowIndex() == 61 || cell.getRowIndex() == 63 || cell.getRowIndex() == 63
        || cell.getRowIndex() == 66 || cell.getRowIndex() == 70 || cell.getRowIndex() == 72){
            font.setColor(IndexedColors.WHITE.getIndex());
            cellStyle.setFont(font);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        }

        /**
         * 添加合并的内容
         */
        List<CellRangeAddress> list = new ArrayList<>();
        CellRangeAddress cellRangeAddress1 = new CellRangeAddress(0,0,0,1);
        CellRangeAddress cellRangeAddress2 = new CellRangeAddress(40,49,0,0);
        CellRangeAddress cellRangeAddress3 = new CellRangeAddress(51,60,0,0);
        list.add(cellRangeAddress1);
        list.add(cellRangeAddress2);
        list.add(cellRangeAddress3);

        /**
         * 判断list集合是否为空后 合并单元格
         */
        if (CollectionUtils.isNotEmpty(list)) {
            for (CellRangeAddress item : list) {
                sheet.addMergedRegionUnsafe(item);
            }
        }

        /**
         * 头信息格式更改
         */
        if(cell.getRowIndex() == 0){
            font.setFontHeightInPoints((short) 12);
            font.setBold(true);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setFont(font);
        }else{
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            if(cell.getColumnIndex() == 1){
                cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);
            }
        }

        cell.setCellStyle(cellStyle);
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    }
}
